package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.system.domain.Drtypeinfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.RegGroupMapper;
import com.ruoyi.system.domain.RegGroup;
import com.ruoyi.system.service.IRegGroupService;

/**
 * 变量分组Service业务层处理
 * 
 * @author yaozh
 * @date 2024-02-29
 */
@Service
public class RegGroupServiceImpl implements IRegGroupService 
{
    private static final Logger log = LoggerFactory.getLogger(RegGroupServiceImpl.class);
    @Autowired
    private RegGroupMapper regGroupMapper;

    /**
     * 查询变量分组
     * 
     * @param id 变量分组主键
     * @return 变量分组
     */
    @Override
    public RegGroup selectRegGroupById(Long id, String dbName)
    {
        return regGroupMapper.selectRegGroupById(id,dbName);
    }

    /**
     * 查询变量分组列表
     * 
     * @param regGroup 变量分组
     * @return 变量分组
     */
    @Override
    public List<RegGroup> selectRegGroupList(RegGroup regGroup, String dbName)
    {
        return regGroupMapper.selectRegGroupList(regGroup,dbName);
    }

    @Override
    public List<RegGroup> selectRegGroupForAll(String dbName)
    {
        return regGroupMapper.selectRegGroupForAll(dbName);
    }

    /**
     * 新增变量分组
     * 
     * @param regGroup 变量分组
     * @return 结果
     */
    @Override
    public int insertRegGroup(RegGroup regGroup, String dbName)
    {
        return regGroupMapper.insertRegGroup(regGroup,dbName);
    }

    /**
     * 修改变量分组
     * 
     * @param regGroup 变量分组
     * @return 结果
     */
    @Override
    public int updateRegGroup(RegGroup regGroup, String dbName)
    {
        return regGroupMapper.updateRegGroup(regGroup,dbName);
    }

    /**
     * 批量删除变量分组
     * 
     * @param ids 需要删除的变量分组主键
     * @return 结果
     */
    @Override
    public int deleteRegGroupByIds(Long[] ids, String dbName)
    {
        return regGroupMapper.deleteRegGroupByIds(ids,dbName);
    }

    /**
     * 删除变量分组信息
     * 
     * @param id 变量分组主键
     * @return 结果
     */
    @Override
    public int deleteRegGroupById(Long id, String dbName)
    {
        return regGroupMapper.deleteRegGroupById(id,dbName);
    }

    @Override
    public String importRegGroup(List<RegGroup> regGroupList, Boolean isUpdateSupport, String operName, String dbName){
        if (regGroupList == null || regGroupList.size() == 0)
        {
            throw new ServiceException("导入项目变量分组数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (RegGroup regGroup : regGroupList)
        {
            try{
                // 验证是否存在
                String groupName = regGroup.getGroupName();
                RegGroup tempRegGroup = regGroupMapper.selectRegGroupByGroupName(groupName,dbName);
                if (tempRegGroup == null)
                {
                    regGroup.setCreateBy(operName);
                    regGroupMapper.insertRegGroup(regGroup,dbName);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、分组名称 " + regGroup.getGroupName() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    regGroup.setId(tempRegGroup.getId());
                    regGroup.setUpdateBy(operName);
                    regGroupMapper.updateRegGroup(regGroup,dbName);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、分组名称 " + regGroup.getGroupName() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、分组名称 " + regGroup.getGroupName() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、分组名称 " + regGroup.getGroupName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }

        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
